<?php
	require_once('init.php');
	
	//Get Current Page
	if (! isset($_GET['page']) || $_GET['page'] == '') {$page = '1';} 
	else {$page = $_GET['page'];}
	
	//Set Shown Form Default New
	$isShow = 'N';
	
	//Get State List
	$stateArray = array();
	$stateArray[0]="已保存";
	$stateArray[1]="已提交";
	$stateArray[2]="已确认";
	$stateArray[3]="合同初稿";
	$stateArray[4]="客服联系";
	$stateArray[5]="书面合同/未付款";
	$stateArray[6]="付款/未发货";
	$stateArray[7]="发货";
	$stateArray[8]="完成";
	$stateArray[9]="已过期";
	
	$smarty->assign('arrayStateOption', $stateArray);
	
	if (isset($_GET['f']) && $_GET['f'] == 'edit'|| (isset($_POST['f']) && $_POST['f'] == 'edit')) 
	{
		//Get Current OrderID
		if (isset($_GET['id'])) 
		{
			$id = $_GET['id'];
		}
		else 
		{
			$id = $_POST['id'];
		}
		
		//Confirm Post
		if (isset($_POST['f']) && $_POST['f'] == 'edit') 
		{		
			//Try Update Order
			if (! $adminDB->executeSQL("update ".$util->contentdbName().".tbOrder set fdStatus = ".$_POST['state']." where id=".$id."", $connID)) 
			{
				echo "<script>alert('订单信息更改失败！');</script>";
			}
			else 
			{
				//Insert AdminLog
				if(!$adminDB->executeSQL("insert into ".$util->userdbName().".tbAdminLog (fdOperatorID,fdAction,fdTarget,fdTime) values('".$_SESSION['adminid']."', 'UPDATE', '".$util->contentdbName().".tbOrder set fdStatus=".$_POST['state']." where id=".$id."' , now())", $connID))
				{
					echo "<script>alert('订单信息更改成功！管理日志添加失败');</script>";
				}
				else
				{			
					echo "<script>alert('订单信息更改成功！');</script>";
				}
			}
		}	
		
		//Query Current Order
		$order = $adminDB->executeSQL("select o.id,o.fdSyncID,o.fdCreate,o.fdSubmit,o.fdStatus,o.fdClient,o.fdClientPhone,o.fdClientAddress,o.fdEmail,o.fdMemo,o.fdUserName,o.fdUserPhone,o.fdUserEmail,o.fdUserTelephone,o.fdUserCompany,o.fdUserPosition,o.fdUserAddress,u.fdName,u.fdLogin,u.fdCompany,u.fdPosition, m.fdPhone as fdMobilePhone, t.fdPhone as fdTelePhone from ".$util->contentdbName().".tbOrder o left outer join ".$util->userdbName().".tbUser u on o.fdUserID = u.id left outer join ".$util->userdbName().".tbPhone m on m.fdUserID = u.id and m.fdTypeID = 1 left outer join ".$util->userdbName().".tbPhone t on t.fdUserID = u.id and t.fdTypeID = 0 where o.id='".$id."'", $connID);
		//Assign Current Order
		$smarty->assign('order', $order);
		
		//Query Order Items
		$itemsql = "select i.id, i.fdOrderID,i.fdProductID,i.fdDate,i.fdPrice,i.fdCount,i.fdStatus,p.fdName from ".$util->contentdbName().".tbItem i left outer join ".$util->contentdbName().".tbProduct p on p.id = i.fdProductID where i.fdOrderID = ".$id."";
		$items = $adminDB->executeSQL($itemsql, $connID);
		$smarty->assign('items', $items);
		
		
		//Set Shown Form Edit
		$isShow = 'E';
	}
	
	
	//Query Order List
	$ordersql = "select o.id,o.fdSyncID,o.fdCreate,o.fdSubmit,o.fdStatus,u.fdLogin from ".$util->contentdbName().".tbOrder o left outer join ".$util->userdbName().".tbUser u on o.fdUserID = u.id ";
	$orders = $pageDB->pageData($ordersql, $connID, 20, $page);	
	$smarty->assign('orders',$orders);	
	$smarty->assign('isShow', $isShow);
	$smarty->display('admin_listorder.html');
	
	$connDB->closeConnID();
	
?>